<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\common\BaseController;
use App\Http\Controllers\Controller;
use App\Models\House;
use App\Services\DataChange;
use Illuminate\Http\Request;
use App\Models\HouseType;
use App\Models\RentHouse;
use App\Service\Es;

class HouseController extends BaseController
{
    //在类的内部可以调用外部不能可以被继承并且重构
    protected $model = House::class;

    /**
     * 查询房源基本信息
     */
    public function houseProperty()
    {
        //查询全部的数据
        $data = House::all();
        $houseRoom = House::first()->houseAttr;
        return $this->retJson('200', '查询成功', ['data' => $data, 'house_room' => $houseRoom]);
    }

    public function getHouse(Request $request)
    {
        $keyword = $request->get('keyword');
        if (!isset($keyword)) {
            $where = [];
            $data = House::getHouseData($where);
        } else {
            $esService = new Es();
            $data = $esService->getData($keyword);
        }
        return $data;
    }

    public function screenHouse(Request $request)
    {
        $params = $request->all();
        $price_scope_id = $params['price_scope_id'];
        $filed_name = $params['filed_name'];
        $house_type_id = $params['house_type_id'];
        $house_direction_id = $params['house_direction_id'];
        $houses_name = $params['houses_name'];
        $where = [];
        if (isset($filed_name)){
            array_push($where,[$filed_name,'=',$house_type_id]);
        }
        if (isset($house_direction_id)){
            array_push($where,['house_direction','=',$house_direction_id]);
        }
        $between = DataChange::priceScope($price_scope_id);
        $data = House::houseScope($between,$houses_name,$where);
        return ['code'=>10001,'msg'=>'查询成功','data'=>$data];
    }

    public function getHouseType()
    {
        $houseType = HouseType::getType();
        return $houseType;
    }

    public function getBagRent(Request $request)
    {
        $house_type = $request->get('house_type_id');
        $regionalism = $request->get('regionalism');
        $where = [];
        if (isset($house_type)) {
            array_push($where, ['house_type','=',$house_type]);
        }
        if (isset($regionalism)) {
            array_push($where, ['regionalism','like',"%$regionalism%"]);
        }
        $rentData = RentHouse::getRentData($where)->toArray();
        $rentData = DataChange::diaryList($rentData);
        return ['code'=>10001,'msg'=>'查询成功','data'=>$rentData];
    }
}
